<?php

namespace App\Library\Traits\Orm;

use App\Library\Logic\LogisticsLogic;
use App\Library\Logic\ShopUserAddressLogic;
use App\Models\PaymentModel;
use App\Models\PaymentRelatedModel;
use App\Models\ShopExpressDeliveryModel;
use App\Models\ShopOrderGoodsModel;
use App\Models\ShopOrderModel;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;

trait ShopOrderReturnOrmTrait
{
    public function good(): BelongsTo
    {
        return $this->belongsTo(ShopOrderGoodsModel::class, 'order_id', 'order_id')->select([
            'id', 'order_id', 'price', 'title', 'spec', 'product_file_id', 'spec_file_id',
        ]);
    }

    public function express(): BelongsTo
    {
        return $this->belongsTo(ShopExpressDeliveryModel::class, 'or_id', 'ed_source_id')->where('ed_type', 2);
    }

    public function order(): BelongsTo
    {
        return $this->belongsTo(ShopOrderModel::class, 'order_id', 'order_id');
    }

    public function payment(): BelongsTo
    {
        return $this->belongsTo(PaymentModel::class, 'order_id', 'object_id')->where('object_type', 'shop_order');
    }

    public function paymentRelated(): BelongsTo
    {
        return $this->belongsTo(PaymentRelatedModel::class, 'order_id', 'object_id')->where([
            'object_type' => 'shop_order',
            'status_pay' => 1,
        ]);
    }

    public function refundAddress(): HasOne
    {
        return self::hasOne(ShopUserAddressLogic::class, 'user_id', 'sell_user_id')
            ->where('ua_is_refund', 1);
    }

    public function logistics(): HasMany
    {
        return $this->hasMany(LogisticsLogic::class, 'object_id', 'or_id')->where('object_type', 1)
            ->where('type', 1);
    }

    public function oneLogistics(): hasOne
    {
        return $this->hasOne(LogisticsLogic::class, 'object_id', 'or_id')->where('object_type', 1)
            ->where('type', 1);
    }

    public function goods(): HasMany
    {
        return $this->hasMany(ShopOrderGoodsModel::class, 'order_id', 'order_id');
    }
}
